package com.taobao.client.component.templete;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * 采用xmlHttpRequest请求方式的HTML模板
 * @author 雕戈
 * 2008-12-19
 * @deprecated
 */
public class HTMLTemplatePanelY extends HTMLTemplateBase{


	/**
	 * 采用xmlHttpRequest请求方式的HTML模板
	 * @param htmlURL
	 * @param divID
	 */
	public HTMLTemplatePanelY(String htmlURL,final String divID) {
		super("Loading....");
		
		loadXMLFile(htmlURL, "", new RequestCallback() {

					public void onError(Request request, Throwable exception) {
						DOM.setInnerHTML(getElement(), exception.getMessage());
					}

					public void onResponseReceived(Request request,Response response) {
						String text = response.getText();
						Element el = getElement();
						DOM.setInnerHTML(el, text);
						Element child;
						int count = DOM.getChildCount(el);
						for (int i = 0; i < count; i++) {
							child = DOM.getChild(el, i);
							if(divID.equals(child.getId())){
								text = child.getString();
								break;
							}
						}
						DOM.setInnerHTML(getElement(), text);
						setTemplateContent();
					}

		});
	}

	public static void loadXMLFile(String url, String requestData,
			RequestCallback requestCallback) {
		url = GWT.getHostPageBaseURL()+url;
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		builder.setHeader("Content-type", "application/x-www-form-urlencoded");
		try {
			builder.sendRequest(requestData, requestCallback);
		} catch (RequestException e) {
			e.printStackTrace();
		}
	}
}
